var t = require;
var i = module;
window.AD_TouTiao = {
    bannerID_TT: "9h682j6d726cmq3sn4",
    videoID_TT: "28ji30dpf7im25mfng",
    chaPing_TT: "2gok9f721elg8g444a",
    shareID_TT: "1s2hlj173saepgol9s",
    appName: "破屋取暖模拟",
    shareTitle_TT: "快来帮助美女修屋取暖",
    shareDESC_TT: "快来帮助美女修屋取暖",
    phone: "null",
    transcribeBoo: !1,
    fenXiangBoo: !1,
    autoVideoBoo: !1,
    fenXiangImgBoo: !1,
    autoVideoNum: 0,
    TTBanner: null,
    switchOn: !1,

    isExist: false,
    isSidEnter:false,
    freeAdNum:0,

    switchOnFunc: function () {
        this.switchOn = !0;
    },

    createGridGamePanel: function () {
        // 单宫格可设置大小和位置
        this.gridGamePanel = window.tt.createGridGamePanel({
            gridCount: "one",
            size: "large",
            position: {
                top: 450,
                left: 0,
            },
            query: {
                tt721a04a7ed2ef09402: "start=2&name=全民破屋",
            },
        });
    },
    showMoreGame: function() {
        if(this.gridGamePanel) {
            this.gridGamePanel.show();
        }
    },
    chaPing: function () {
        var t;
        AD.chanelName == AD.chanelName1 &&
            (console.log("显示插屏switchOn" + this.switchOn),
            console.log("显示插屏"),
            "ios" !== tt.getSystemInfoSync().platform) &&
            ((t = tt.createInterstitialAd({adUnitId: AD_TouTiao.chaPing_TT})),
            console.log("进来了"),
            t &&
                (t.load()
                    .then(function () {
                        t.show().then( () =>{
                            console.log("成功调取插屏");
                        });
                    })
                    .catch(function (t) {
                        console.log("插屏错误  " + JSON.stringify(t));
                    }),
                t.onClose(function () {
                    console.log("插屏关闭  后销毁 ");
                }),
                t.onError(function (t) {
                    console.log("插屏错误1   " + JSON.stringify(t));
                })));
    },
    shiPin: function () {
        var n, e, o, c, s;
        if(AD_TouTiao.freeAdNum > 0){
            AD.reward()
            AD_TouTiao.freeAdNum = AD_TouTiao.freeAdNum-1;
            console.log("免费次数： " + AD_TouTiao.freeAdNum)
            return
        }
        AD.chanelName == AD.chanelName1
            ? ((n = 0),
              (e = tt.createRewardedVideoAd({adUnitId: AD_TouTiao.videoID_TT}))
                  .load()
                  .then(function () {
                      e.show();
                  })
                  .catch(function (t) {
                      console.log(t.errMsg);
                  }),
              (o = function () {
                  console.log("sdk videoAd start ........");
              }),
              (c = function t(i) {
                  console.log(JSON.stringify(i)),
                      0 <= n && n < 1
                          ? (e
                                .load()
                                .then(function () {
                                    e.show();
                                })
                                .catch(function (t) {
                                    console.log(t.errMsg), cc.director.resume();
                                }),
                            n++)
                          : 1 <= n &&
                            (e.offLoad(o),
                            e.offClose(s),
                            e.offError(t),
                            console.log("miaoju_watch_retry_times" + n),
                            (n = -1));
              }),
              (s = function t(i) {
                  console.log("临时关闭视频：" + i),
                      i.isEnded
                          ? (console.log("视频成功了，走成功回调"), cc.director.resume(), AD.reward())
                          : (console.log("视频失败了，走失败回调"), cc.director.resume()),
                      e.offLoad(o),
                      e.offClose(t),
                      e.offError(c);
              }),
              e.onLoad(o),
              e.onError(c),
              e.onClose(s))
            : AD.reward();
    },
    initBanner: function () {
        var t, i, n;
        AD.chanelName == AD.chanelName1 &&
            (console.log("头条banner init"),
            (t = tt.getSystemInfoSync()),
            (i = t.windowWidth),
            (n = t.windowHeight),
            (AD_TouTiao.TTBanner = tt.createBannerAd({
                adUnitId: AD_TouTiao.bannerID_TT,
                adIntervals:40,
                style: {top: 100},
            })),
            console.log("屏幕尺寸 宽度：" + i + "高度：   " + n),
            console.log("头条banner init结束"),
            AD_TouTiao.TTBanner.onResize( (t) => {
                console.log("改变size"),
                    (AD_TouTiao.TTBanner.style.left = (i - t.width) / 2),
                    (AD_TouTiao.TTBanner.style.top = n - t.height);
            }),
            AD_TouTiao.TTBanner.onLoad( () => {
                console.log("banner 下载成功")
                AD_TouTiao.TTBanner.show()
                    .then(function () {
                        console.log("广告显示成功");
                    })
                    .catch(function (t) {
                        console.log("banner load错误 : ", t);
                    });
            }),
            AD_TouTiao.TTBanner.onError(function (t) {
                console.log("banner 下载失败" + JSON.stringify(t));
            }));
    },
    showBanner: function () {
        AD.chanelName == AD.chanelName1 &&
            (console.log("显示banner switchOn" + this.switchOn), AD_TouTiao.initBanner());
    },
    hideBanner: function () {
        AD.chanelName == AD.chanelName1 && null != AD_TouTiao.TTBanner && AD_TouTiao.TTBanner.destroy();
    },
    luPingBegin: function () {
        var t;
        "touTiao" == AD.chanelName1 &&
            AD.chanelName == AD.chanelName1 &&
            (console.log("录屏开始"),
            console.log("luping  開始"),
            (AD_TouTiao.transcribeBoo = !1),
            (AD_TouTiao.fenXiangBoo = !1),
            (AD_TouTiao.resVideoPathTouTiao = null),
            (t = tt.getGameRecorderManager()) &&
                (tt.onShow(function () {
                    t.resume();
                }),
                tt.onHide(function () {
                    t.pause();
                }),
                t.onStart(function () {
                    console.log("录屏开始");
                }),
                t.start({duration: 300})));
    },
    luPingOver: function () {
        var t;
        "touTiao" == AD.chanelName1 &&
            AD.chanelName == AD.chanelName1 &&
            1 != AD_TouTiao.transcribeBoo &&
            ((AD_TouTiao.transcribeBoo = !0), console.log("录屏结束"), AD.chanelName == AD.chanelName1) &&
            (console.log("luping  結束"),
            (t = tt.getGameRecorderManager()) &&
                (tt.offShow(function () {}),
                tt.offHide(function () {}),
                t.onStop(function (t) {
                    (AD_TouTiao.resVideoPathTouTiao = t.videoPath),
                        console.log("录屏结束：" + t.videoPath),
                        console.log("录屏结束：" + t);
                }),
                t.stop()));
    },
    luPingShare: function () {
        "touTiao" == AD.chanelName1 &&
            AD.chanelName == AD.chanelName1 &&
            null != AD_TouTiao.resVideoPathTouTiao &&
            (cc.log("分享视频 = " + AD_TouTiao.resVideoPathTouTiao),
            tt.shareAppMessage({
                channel: "video",
                query: "",
                templateId: AD_TouTiao.shareID_TT,
                title: AD_TouTiao.shareTitle_TT,
                desc: AD_TouTiao.shareDESC_TT,
                extra: {
                    videoPath: AD_TouTiao.resVideoPathTouTiao,
                    videoTopics: [AD_TouTiao.appName, "抖音小游戏"],
                    hashtag_list: [AD_TouTiao.appName, "抖音小游戏"],
                    video_title: AD_TouTiao.shareDESC_TT
                },
                success: function () {
                    console.log("分享视频成功"), (AD_TouTiao.fenXiangBoo = !1), cc.director.emit("分享成功");
                },
                fail: function (t) {
                    console.log(t),
                        console.log("分享视频失败"),
                        console.log("e.errMsg :" + t.errMsg),
                        !t.errMsg ||
                            ("shareAppMessage:cancel" == t.errMsg &&
                                "shareAppMessage:fail cancel" == t.errMsg &&
                                tt.showToast({
                                    title: "分享失败时长过低",
                                    duration: 2e3,
                                    success: function (t) {
                                        console.log("" + t);
                                    },
                                    fail: function () {
                                        console.log("showToast调用失败");
                                    }
                                }));
                }
            }));
    },
    shareTTNormal: function () {
        AD.chanelName == AD.chanelName1 &&
            (cc.log("分享视频 = " + AD_TouTiao.resVideoPathTouTiao),
            tt.shareAppMessage({
                channel: "",
                query: "",
                templateId: AD_TouTiao.shareID_TT,
                title: AD_TouTiao.shareTitle_TT,
                desc: AD_TouTiao.shareDESC_TT,
                extra: {
                    videoPath: AD_TouTiao.resVideoPathTouTiao,
                    videoTopics: [AD_TouTiao.appName, AD_TouTiao.shareDESC_TT, "抖音小游戏"]
                },
                success: function () {},
                fail: function (t) {
                    console.log(t),
                        console.log("分享视频失败"),
                        t.errMsg &&
                            "shareAppMessage:cancel" != t.errMsg &&
                            cc.director.emit("系统提示", "录屏失败：录屏时长低于 3 秒");
                }
            }));
    },
    addDesktop: function () {
        tt.addShortcut({
            success: function () {
                console.log("添加桌面成功");
            },
            fail: function (t) {
                console.log("添加桌面失败", JSON.stringify(t)),
                    tt.showToast({
                        title: "添加桌面失败",
                        duration: 2e3,
                        success: function (t) {
                            console.log("" + t);
                        },
                        fail: function (t) {
                            console.log("桌面:", t.errMsg);
                        }
                    });
            }
        });
    },
    showBtnAddToDesk: function () {
        if (AD.chanelName == AD.chanelName1)
            try {
                var t = tt.getSystemInfoSync();
                console.log("手机型号为 " + t.model),
                    "android" == t.platform &&
                        tt.checkShortcut({
                            success: function (t) {
                                return (
                                    console.log("检查快捷方式", t.status),
                                    (0 != t.status.exist && !t.status.needUpdate) || (AD.cc = !0),
                                    !1
                                );
                            },
                            fail: function (t) {
                                return console.log("检查快捷方式失败", t.errMsg), !1;
                            }
                        });
            } catch (t) {
                return console.log("获取系统信息失败"), !1;
            }
    },
    //打开侧边栏
    navigateToScene: function(){
        if (cc.sys.platform === cc.sys.BYTEDANCE_GAME){
            tt.navigateToScene({
                scene: "sidebar",
                success: (res) => {
                    console.log("navigate to scene success");
                    // 跳转成功回调逻辑
                },
                fail: (res) => {
                    console.log("navigate to scene fail: ", res);
                    // 跳转失败回调逻辑
                },
            });
        } 
    },

    checkIsExist: function(e){
        if ( cc.sys.platform === cc.sys.BYTEDANCE_GAME){
            tt.checkScene({
                scene: "sidebar",
                success: (res) => {
                    console.log("check scene success: ", res.isExist);
                    AD_TouTiao.isExist = res.isExist;
                    e();
                },
                fail: (res) => {
                    console.log("check scene fail:", res);
                }
            });
        }
    }
};
